package com.yq.tanhua.ui

import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.fmt.mvi.learn.gobal.log
import com.yq.tanhua.net.vo.Movment
import com.yq.tanhua.utils.UserCenter

@Composable
fun MovementItemView(movment: Movment) {

    Column(
        modifier = Modifier.padding(
            10.dp
        )
    ) {
        HeadArea(movment = movment)
        Text(text = movment.textContent, modifier = Modifier.padding(vertical = 12.dp))

        if (movment.imageContent.size == 1) {
            val pic = movment.imageContent[0]
            AsyncImage(
                model = pic,
                contentScale = ContentScale.Fit,
                contentDescription = "Translated description of what the image contains"
            )
        } else if (movment.imageContent.size <= 3) {
            Row {
                movment.imageContent.forEach {
                    AsyncImage(
                        model = it,
                        contentScale = ContentScale.FillWidth,
                        modifier = Modifier
                            .padding(4.dp)
                            .weight(1f)
                            .height(120.dp),
                        contentDescription = "Translated description of what the image contains"
                    )
                }
            }
        } else {
            //TODO 这种列表嵌套列表的高度咋整？
            val lines = movment.imageContent.size / 3
            Box(modifier = Modifier.height(((lines + 1) * 100).dp)) {
                LazyVerticalGrid(
                    columns = GridCells.Fixed(3), content = {
                        itemsIndexed(movment.imageContent) { index, item ->
                            AsyncImage(
                                model = item,
                                modifier = Modifier
                                    .padding(vertical = 5.dp)
                                    .width(40.dp)
                                    .height(90.dp),
                                contentDescription = "Translated description of what the image contains"
                            )
                        }
                    })
            }


//            Row {
//                movment.imageContent.forEach {
//                    AsyncImage(
//                        model = it,
//                        contentScale = ContentScale.FillWidth,
//                        modifier = Modifier
//                            .padding(4.dp)
//                            .weight(1f)
//                            .height(120.dp),
//                        contentDescription = "Translated description of what the image contains"
//                    )
//                }
//            }
        }


    }
}

@Composable
private fun HeadArea(movment: Movment) {
    var userInfo = UserCenter.userInfo
    Row {
        AsyncImage(
            model = movment.avatar,
            contentDescription = "",
            modifier = Modifier
                .width(44.dp)
                .height(44.dp)
        )
        Spacer(modifier = Modifier.width(12.dp))
        Column {
            Row {
                if (userInfo?.id == movment.userId) {
                    Text(text = "我", style = TextStyle(Color.Red))
                } else {
                    Text(text = movment.nickname.toString())
                }

                Text(text = movment.age.toString(), modifier = Modifier.padding(start = 4.dp))
            }
            Spacer(modifier = Modifier.height(6.dp))
            Row {
                for (tag in movment.tags) {
                    Text(text = tag, modifier = Modifier.padding(end = 6.dp))
                }
            }
        }
    }
}